<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\finance\InvoiceRequest;
use App\Models\finance\InvoiceModel;
use Illuminate\Http\Request;

class Invoice extends Controller
{
    use InvoiceRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $cycle_start = $request->input('cycle_start', '');
        $cycle_end = $request->input('cycle_end', '');
        $customer_id = $request->input('customer_id', '');

        $query = InvoiceModel::query()->with(['create_admin', 'customer']);

        if($customer_id){
            $query->where('customer_id', $customer_id);
        }



        if($cycle_start && $cycle_end){
            $query = $query->where('created_at', '>=', $cycle_start)->where('created_at', '<=', $cycle_end);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer ? $item->customer->name : '',
                'company_name' => $item->company_name,
//                'type' => $item->type,
//                'type_name' => InvoiceModel::TYPE[$item->type],
                'amount' => $item->amount,
//                'tax_number' => $item->tax_number,
//                'open_bank' => $item->open_bank,
//                'bank_account' => $item->bank_account,
//                'company_address' => $item->company_address,
//                'company_mobile' => $item->company_mobile,
                'invoice_date' => $item->invoice_date,
                'cycle_start' => $item->cycle_start,
                'cycle_end' => $item->cycle_end,
                'remarks' => $item->remarks,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new InvoiceModel();
        $model->customer_id = $params['customer_id'];
        $model->company_name = $params['company_name'];
//        $model->type = $params['type'];
//        $model->tax_number = $params['tax_number'];
//        $model->open_bank = $params['open_bank'];
//        $model->bank_account = $params['bank_account'];
//        $model->company_address = $params['company_address'];
//        $model->company_mobile = $params['company_mobile'];
        $model->amount = $params['amount'];
        $model->invoice_date = $params['invoice_date'] ?? '';
        $model->cycle_start = $params['cycle_start'];
        $model->cycle_end = $params['cycle_end'];
        $model->remarks = isset($params['remarks']) ? $params['remarks'] : '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id) {
        $params = $request->all();
        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = InvoiceModel::query()->find($id);
        if(!$model){
            return $this->error('发票不存在');
        }

        $model->customer_id = $params['customer_id'];
        $model->company_name = $params['company_name'];
//        $model->type = $params['type'];
//        $model->tax_number = $params['tax_number'];
//        $model->open_bank = $params['open_bank'];
//        $model->bank_account = $params['bank_account'];
//        $model->company_address = $params['company_address'];
//        $model->company_mobile = $params['company_mobile'];
        $model->amount = $params['amount'];
        $model->invoice_date = $params['invoice_date'] ?? '';
        $model->cycle_start = $params['cycle_start'];
        $model->cycle_end = $params['cycle_end'];
        $model->remarks = isset($params['remarks']) ? $params['remarks'] : '';
        $model->save();

        return $this->success();
    }

    public function delete(Request $request, $id) {
        $model = InvoiceModel::query()->find($id);
        if(!$model){
            return $this->error('发票不存在');
        }
        $model->delete();
        return $this->success();
    }

}
